package com.example.toutiao.ui.main.adpater

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.example.toutiao.R
import com.example.toutiao.ui.main.model.RecommenModel
import com.example.toutiao.ui.main.model.TabListModel

class ChannelAdapter(data: TabListModel) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private val myChannels: MutableList<RecommenModel>? = data.myChannels
    private val channelsRecommend: MutableList<RecommenModel>? = data.channelsRecommend
    private var isEdit: Boolean = false



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view: View
        val layoutInflater: LayoutInflater = LayoutInflater.from(parent.context)
        when (viewType) {
            0 -> {
                view = layoutInflater.inflate(R.layout.item_my_title, parent, false)
                val holder = TitleHolder(view)
                holder.edit.setOnClickListener {
                    if (!isEdit) {
                        startEdit(parent as RecyclerView)
                        holder.edit.text = "完成"
                        holder.desc.text="拖动可编辑"
                    } else {
                        cancelEdit(parent as RecyclerView)
                        holder.edit.text = "编辑"
                        holder.desc.text="点击进入频道"
                    }
                }

                return holder
            }
            1 -> {
                view = layoutInflater.inflate(R.layout.item_rec_title, parent, false)
                return TitleHolder(view)
            }
            2 -> {
                view = layoutInflater.inflate(R.layout.item_my_channel, parent, false)
                val holder = MyHolder(view)
                holder.itemView.setOnClickListener {
                    if (isEdit){
                        myToRecommend(holder)
                    }
                }
                return holder
            }
            else -> {
                view = layoutInflater.inflate(R.layout.item_rec_channel, parent, false)
                val holder = RecommendHolder(view)
                holder.itemView.setOnClickListener {
                    if (isEdit){
                        addMyChannels(holder)
                    }
                }
                return holder
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is MyHolder -> {
                holder.title.text = myChannels!![position - 1].name
                holder.close.isVisible = isEdit
            }
            is RecommendHolder -> {
                holder.title.text = channelsRecommend!![position - myChannels!!.size - 2].name
            }
            else -> {

            }
        }
    }

    override fun getItemCount(): Int = myChannels!!.size + channelsRecommend!!.size + 2

    override fun getItemViewType(position: Int): Int {
        /* 标题占一个元素的位置*/
        return if (position == 0) {
            /*我的频道标题*/
            0
        } else if (myChannels!!.size + 1 == position) {
            /*推荐频道标题*/
            1
        } else if (position > 0 && position < myChannels.size + 1) {
            /* 我的频道 的小标题*/
            2
        } else {
            3
        }
    }

    class TitleHolder(it: View) : RecyclerView.ViewHolder(it) {
        val edit: TextView = it.findViewById(R.id.iv_edit)
        val desc: TextView = it.findViewById(R.id.iv_desc)
    }

    class MyHolder(it: View) : RecyclerView.ViewHolder(it) {
        val close: TextView = it.findViewById(R.id.iv_close)
        val title: TextView = it.findViewById(R.id.iv_title)

    }

    class RecommendHolder(it: View) : RecyclerView.ViewHolder(it) {
        val title: TextView = it.findViewById(R.id.iv_title)
    }

    /*可以编辑的状态*/
    private fun startEdit(parent: RecyclerView) {
        isEdit = true
        for (i: Int in 0 until myChannels!!.size) {
            val view: View = parent.getChildAt(i + 1)
            val ivEdit: TextView = view.findViewById(R.id.iv_close)
            ivEdit.isVisible = true
        }
    }

    /*不可以编辑的状态*/
    private fun cancelEdit(parent: RecyclerView) {
        isEdit = false
        for (i: Int in 0 until myChannels!!.size) {
            val view: View = parent.getChildAt(i + 1)
            val ivEdit: TextView = view.findViewById(R.id.iv_close)
            ivEdit.isVisible = false
        }
    }

    /* 移除我的频道 */
    private fun myToRecommend(holder:MyHolder){
        val position:Int=holder.adapterPosition
        val startPosition:Int=position-1
        if(startPosition>myChannels!!.size-1){
            return
        }
        val item:RecommenModel=myChannels[startPosition]
        myChannels.removeAt(startPosition)
        channelsRecommend!!.add(0,item)
        notifyItemMoved(position,myChannels!!.size+2)
    }
    /* 添加到我的频道 */
    private fun addMyChannels(holder:RecommendHolder){
        val position:Int=getPosition(holder)
        if(position==-1){
            return
        }
        notifyItemMoved(position,myChannels!!.size)
    }
    private fun getPosition(holder:RecommendHolder):Int{
        val position:Int=holder.adapterPosition
        println(position)
        val startPosition:Int=position-2-myChannels!!.size
        if(startPosition>(channelsRecommend!!.size-1)){
            return -1
        }
        val item:RecommenModel=channelsRecommend[startPosition]
        channelsRecommend!!.removeAt(startPosition)
        myChannels!!.add(item)

        return position
    }
}